<?php
/**
 * Created by: i@fedax.cn
 * Date: 2018/7/16 22:39
 */

namespace app\admin\controller;

use app\admin\model\GoldDisplay;
use think\facade\Env;
use app\admin\model\Gift as GiftModel;
use think\Request;
use think\File;
class Gift extends Index
{
    public function index()
    {
        return view();
    }
    public function indexData()
    {
        $giftModel = new GiftModel();
        $data = $giftModel->getDataList();
        return json($data);
    }
    public function add()
    {
        $giftModel = new GiftModel();
        $res = $giftModel->add();
        if($res) $this->success(lang('add') . lang('success'));
        else $this->error(lang('add') . lang('false'));
        return redirect('/Miss');
    }

    public function changeStatus()
    {
        $redis = new \Redis();
        $redis->connect('127.0.0.1','6379');
        $redis->auth('susu123');
        if(request()->isPost()){
            $giftModel = new GiftModel();
            $giftDetail = $giftModel ->where('id='.input('id'))->find();
            $newData['status'] = $giftDetail['status'] === 1?0:1;
            try{
                $giftModel->save($newData,['id' =>input('id')]);
                $redis->del('giftList');
            }catch(\Exception $e){
                $this->error(lang('edit').lang('false').$e->getMessage());
            }
            $this->success(lang('edit').lang('success'));
        }
        return redirect('/Miss');
    }


    //金宝箱礼物展示
    public function boxGift()
    {

        return view();
    }

    //至尊宝箱礼物展示
    public function extremeBox()
    {

        return view();
    }

    //宝箱礼物展示数据
    public function boxGiftData()
    {
        $goldDisplayModel = new GoldDisplay();
        $gold_egg_id = input('gold_egg_id');
        $data = $goldDisplayModel->boxGift($gold_egg_id);
        return json($data);
    }
//宝箱展示添加
    public function boxGiftAdd()
    {
        $newData = input('post.');
        $goldDisplayModel = new GoldDisplay();
        $re = $goldDisplayModel->where($newData)->find();
        if($re) return ['code'=>-100,'msg'=>'奖品已存在'];
        $res= $goldDisplayModel->save($newData);
        if($res) return ['code'=>100,'msg'=>'添加成功'];
        else return ['code'=>-100,'msg'=>'添加失败'];
    }

    //修改概率
    public function boxGiftEdit()
    {
        $newData = input('get.');
        // halt($newData);
        $goldDisplayModel = new GoldDisplay();
        if($newData['rate'] < 0 || $newData['rate'] > 100)  return ['code'=>-100,'msg'=>'概率不合法'];
        $res = $goldDisplayModel->save(['rate'=>$newData['rate']],['id' =>$newData['id']]);
        if($res) return ['code'=>100,'msg'=>'修改成功'];
        else return ['code'=>-100,'msg'=>'修改失败'];
    }

    public function boxGiftDel()
    {
        $id = input('id');
        $goldDisplayModel = new GoldDisplay();
        $re = $goldDisplayModel->where(['id'=>$id])->delete();
        if($re) return ['code'=>100,'msg'=>'删除成功'];
        else return ['code'=>-100,'msg'=>'删除失败'];
    }

    public function boxGiftDetail()
    {
        $type = input('type');
//        $page_config = array(
//            'page' => input('offset', 1),
//            'list_rows' => input('limit',10000),
//        );
        $data = [];
        if($type==1){
            $gift = new \app\admin\model\Gift();
            $data =  $gift->field('id,gift_name,diamonds')
                ->order('diamonds DESC')
                ->select();
        }
        if($type==2){
            $prop = new \app\admin\model\Prop();
            $data =  $prop
                ->field('id,prop_name')
                ->select();
        }

        return $data;

    }
}